package com.csx.governance.traffic.core.context;

import java.util.List;
import java.util.Map;

/**
 * @author cuisongxu
 * @date 2022/12/3 17:22
 */
public interface TrafficMessage {

    /**
     * 根据key获取属性值
     * @param key
     * @return
     */
    Object getAttribute(String key);

    TrafficMessage setAttribute(String key, Object value);

    void removeAttribute(String key);

    default <T> T getAttribute(String key, Class<T> type) {

        Object attribute = getAttribute(key);
        if(attribute == null || type == null) {
            return null;
        }
        if(type.isInstance(attribute)) {
            return type.cast(attribute);
        }
        return null;
    }

    default <T> T getAttribute(String key, Class<T> type, T defaultValue) {

        T attribute = getAttribute(key, type);
        return attribute != null ? attribute : defaultValue;
    }

    Map<String, Object> getAttributes();

    default List<String> getHeaders(String name) {
        throw new UnsupportedOperationException();
    }

    default String getHeader(String name) {
        throw new UnsupportedOperationException();
    }

    void setHeader(String name, String value);

    Map<String, String> getSingleValueHeaders();

    String getFirstHeader(String name);

    <T> T getOriginMessage(Class<T> desiredType);
}

